package com.shop.controller.admin;
import java.sql.SQLException;

import com.jf.core.BaseController;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.shop.bean.NzcpdlBean;
import com.shop.bean.NzcpxlBean;
import com.shop.utils.StringUtils;

public class CategoriesController extends BaseController{
	/**
	 * 获取所有的商品大类
	 * */
	public void getCpdls(){
		String select = "select *";
		String sqlExceptSelect = "from tblnzcpdl";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		} 
		Page<NzcpdlBean> cpdls = NzcpdlBean.cpdlDao.paginate(page, 55, select,sqlExceptSelect);
		setAttr("cpdls", cpdls);
		setAttr("TotalRow", cpdls.getTotalRow());
		setAttr("PageNumber",cpdls.getPageNumber());
		setAttr("cpdlslist",cpdls.getList());
		render(WEB_INF_URL+"admin/files/listcpdls.jsp");
	}
	
	/**
	 * 获取所有的商品小类
	 * */
	public void getCpxls(){
		String dlcode = getPara("dlcode");
		String select = "select *";
		String sqlExceptSelect = "from tblnzcpxl where dlcode='"+dlcode+"'";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<NzcpxlBean> cpxls = NzcpxlBean.cpxlDao.paginate(page, 55, select,sqlExceptSelect);
		setAttr("cpxls", cpxls);
		setAttr("dlcode",dlcode);
		render(WEB_INF_URL+"admin/files/listcpxls.jsp");
	}
	
	/**
	 * 增加小类
	 * */
	public void addCdxl(){
		String dlcode = getPara("dlcode");
		//String sql_ = StringUtils.getSql("sql.find.findDlnameByDlcode", dlcode);
		NzcpdlBean cpdl = NzcpdlBean.cpdlDao.findById(dlcode);
		setAttr("cpdl",cpdl);
		render(WEB_INF_URL+"admin/files/addCpxl.jsp");
	}
	
	/**
	 * 提交增加小类方法
	 * */
	public void addCdxlComm(){
		String dlcode = getPara("dlcode");
		String cpxlName = getPara("cpxlName");
		NzcpxlBean cpxl = new NzcpxlBean();
		String xlcode = StringUtils.getTimeCode();
		boolean bool = cpxl.set("xlcode", xlcode).set("xlname", cpxlName)
				.set("dlcode", dlcode).save();
		if(bool){
			redirect("getCpxls?dlcode="+dlcode);
		}else{
			
		}
	}
	
	/**
	 * 删除小类
	 * */
	public void delCpxl(){
		String xlcode = getPara("xlcode");
		boolean bool = NzcpxlBean.cpxlDao.deleteById(xlcode);
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 删除所有选择的小类
	 * */
	public void delCpxlList(){
		final String xlcodeList = getPara("xlcodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String[] xlcodeArr = xlcodeList.split("@");
				for(String xlcode: xlcodeArr){
					result = NzcpxlBean.cpxlDao.deleteById(xlcode);
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
}
